package cn.ncist.health_service_provider.service.impl;

import cn.ncist.entity.QueryPageBean;
import cn.ncist.health_interface.service.CheckGroupService;
import cn.ncist.health_service_provider.dao.CheckGroupMapper;
import cn.ncist.pojo.CheckGroup;
import cn.ncist.pojo.CheckItem;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Transactional
@DubboService(interfaceClass = CheckGroupService.class,timeout = 50000)
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public Page<CheckGroup> findPage(QueryPageBean queryPageBean) {
        Page<CheckGroup> pageInfo = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        LambdaQueryWrapper<CheckGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0
                        ,CheckGroup::getCode
                        ,queryPageBean.getQueryString())
                .or()
                .eq(queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0
                        ,CheckGroup::getName
                        ,queryPageBean.getQueryString())
                .or()
                .eq(queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0
                        ,CheckGroup::getHelpCode
                        ,queryPageBean.getQueryString());
        this.page(pageInfo,queryWrapper);
        //根据checkGroupId去查询关联的checkItem
        List<CheckGroup> checkGroupList = pageInfo.getRecords();
        for (CheckGroup checkGroup : checkGroupList) {
            Integer checkGroupId = checkGroup.getId();
            List<CheckItem> checkItemList = checkGroupMapper.selectCheckItemFromTableCheckItem(checkGroupId);
            checkGroup.setCheckItems(checkItemList);
        }
        return pageInfo;
    }

    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
//        checkGroupMapper.addCheckGroup(checkGroup);
        log.info("checkGroupServiceImpl被执行一次");
        this.save(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        log.info("检查组的id为{}",checkGroupId);
        this.setCheckItemAndCheckGroup(checkGroupId, checkitemIds);
    }

    @Override
    public List<Integer> selectCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        List<Integer> checkItemIds = checkGroupMapper.selectCheckItemIdsFromCheckGroupCheckItemTable(checkGroupId);
        return checkItemIds;
    }

    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        this.updateById(checkGroup);
        //删除原来地映射关系
        checkGroupMapper.deleteCheckItemIdByCheckGroupId(checkGroup.getId());
        //添加新地映射关系
        this.setCheckItemAndCheckGroup(checkGroup.getId(),checkitemIds);
    }

    @Override
    public void delete(Integer checkGroupId) {
        removeById(checkGroupId);
        checkGroupMapper.deleteCheckItemIdByCheckGroupId(checkGroupId);
    }

    @Override
    public List<CheckGroup> findAll() {
        return this.list();
    }

    /**
     * 设置CheckGroupId和CheckItemId的映射关系
     * 并向中间表t_checkgroup_checkitem表添加关联数据
      * @param checkGroupId
     * @param checkitemIds
     * @return
     */
    public void setCheckItemAndCheckGroup(Integer checkGroupId,Integer[] checkitemIds){
        Map<String, Integer> map = new HashMap<>();
        for (Integer checkitemId : checkitemIds) {
            map.put("checkGroupId",checkGroupId);
            map.put("checkItemId",checkitemId);
            checkGroupMapper.setCheckItemsIdAndCheckGroupId(map);
        }
    }
}
